<?php
function TopProdInsert($OH_ID){

$order_top_sql= "SELECT OP_ID,OP_Count FROM order_products WHERE (OH_ID =".$OH_ID.")";
$order_top=mysql_query($order_top_sql);
while($order_top_arr = mysql_fetch_object($order_top))
		{
		if($order_top_arr->OP_ID!="")
		{
		  	$sql_psel= "SELECT COUNT(*) FROM top_products WHERE ID_top=".$order_top_arr->OP_ID;
    		$sql_array_Prodct = mysql_fetch_row(mysql_query($sql_psel));
			if($sql_array_Prodct[0]!=0)
    		{	
    		  	$sql_dir= "SELECT Top_Count,ID_top FROM top_products WHERE (ID_top=".$order_top_arr->OP_ID.")";
    			$sql = mysql_fetch_object(mysql_query($sql_dir));
    		  	$Count_Value=($sql->Top_Count)+($order_top_arr->OP_Count);
				  $sql="UPDATE top_products SET Top_Count=".$Count_Value." WHERE ID_top=".$order_top_arr->OP_ID;
    				mysql_query($sql);
					  	
    		}else
    		{
    		  	$sql="INSERT INTO top_products (ID_top,Top_Count) VALUES (".$order_top_arr->OP_ID.",".$order_top_arr->OP_Count.")";
    				mysql_query($sql);
    			
    		}
    	}		
		}
}
function TopProdUnset($OH_ID){

$order_top_sql= "SELECT OP_ID,OP_Count FROM order_products WHERE (OH_ID =".$OH_ID.")";
$order_top=mysql_query($order_top_sql); 	  
while($order_top_arr = mysql_fetch_object($order_top))
		{
		if($order_top_arr->OP_ID!="")
		{
		  	$sql_psel= "SELECT COUNT(*) FROM top_products WHERE ID_top=".$order_top_arr->OP_ID;
    		$sql_array_Prodct = mysql_fetch_row(mysql_query($sql_psel));
			if($sql_array_Prodct[0]!=0)
    		{	
    		  	$sql_dir= "SELECT Top_Count,ID_top FROM top_products WHERE (ID_top=".$order_top_arr->OP_ID.")";
    			$sql = mysql_fetch_object(mysql_query($sql_dir));
    		  	$Count_Value=($sql->Top_Count)-($order_top_arr->OP_Count);
    		  	//print($Count_Value);
    		  		if(!($Count_Value<=0)){
				  		$sql="UPDATE top_products SET Top_Count=".$Count_Value." WHERE ID_top=".$order_top_arr->OP_ID;
    						mysql_query($sql);
					  	
		    		}else
		    		{
		    		  	$sql_table= "DELETE FROM top_products WHERE ID_top=".$order_top_arr->OP_ID;
							mysql_query($sql_table);
		    			
		    		}
    		}
    	}		
		}
}
function Print_Top_Products_Small($FRAME_MENU_WIDTH){
	
	print "<table cellspacing=\"0\" cellpadding=\"0\" style=\"border:0px;\">";
 		
		$sql_top= "SELECT ID_top,Top_Count FROM top_products,products WHERE top_products.ID_top=products.ID ORDER BY Top_Count DESC LIMIT 0,10";
        $Product_source = mysql_query($sql_top);
        
        $sql_top= "SELECT MAX(Top_Count) FROM top_products";
        $top_koeficient_sql = mysql_fetch_row(mysql_query($sql_top));
		if($top_koeficient_sql[0])
		$top_koeficient=$FRAME_MENU_WIDTH/$top_koeficient_sql[0];
        
        $counter=0;
        
        while($sql_array_Prodct = mysql_fetch_object($Product_source)){
       
	   	$sql_top_detail= "SELECT ID,Product_Name,Product_Price,Product_Code FROM products WHERE ID=".$sql_array_Prodct->ID_top;
     	$sql_object = mysql_fetch_object(mysql_query($sql_top_detail));
     		
		  	
	  	$image_name = $_SERVER['DOCUMENT_ROOT']."/admin/img/".$sql_object->ID."mini.jpg";
	  	$image_address = "http://".$_SERVER['HTTP_HOST']."/admin/img/".$sql_object->ID."mini.jpg";
	  	
	  	print "<tr><td>";
			print "
				<table cellspacing=\"0\" cellpadding=\"0\" style=\"border:0px; width:".$FRAME_MENU_WIDTH.";\">
					<tr>
						<td style=\"background-repeat:repeat;
									background-position:0px 0px;
									border-bottom:0px solid black;\"
						>
							<table cellspacing=\"0\" cellpadding=\"0\" style=\"border:0px;\">
								<tr style=\"text-align:left;width:".$FRAME_MENU_WIDTH."\"> 
									<td>
										<img src=\"admin/modules/top_products/baner.jpg\" alt=\"\" style=\"height:10px;width:".$sql_array_Prodct->Top_Count*$top_koeficient."px\"  /><br />
									</td>
								</tr>
								<tr>
									<td>
										<b>
											<a href=\"product_detail.php?ProductID=".$sql_object->ID."\" target=\"products\">
												".$sql_object->Product_Name."
											</a>
										</b>
										<br />									
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			";
		print "</td></tr>";
	}
	print "</table>";	
}
?>